package com.meida.student.app.scholartyrant.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.UpdateInterceptor;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaUpdate;
import com.meida.module.bus.client.entity.BusOrder;
import com.meida.module.bus.client.entity.BusOrderAnswer;
import com.meida.module.bus.client.enums.OrderStatePEnum;
import com.meida.module.bus.client.enums.OrderStateYEnum;
import com.meida.module.bus.client.enums.OrderStateZEnum;
import com.meida.module.bus.client.enums.OrderTypeEnum;
import com.meida.module.bus.provider.service.BusOrderAnswerService;
import com.meida.module.bus.provider.service.BusOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 拍照提问/作业无忧 提交答案
 *
 * @author zyf
 */
@Component
public class OrderAnswerHandler implements UpdateInterceptor<BusOrderAnswer> {

    @Autowired
    private BusOrderService orderService;
    @Autowired
    private BusOrderAnswerService busOrderAnswerService;

    @Override
    public ResultBody validate(CriteriaUpdate cu, EntityMap params) {
        Long answerId = cu.getLong("answerId");
        if (FlymeUtils.isEmpty(answerId)) {
            return ResultBody.failed("缺少answerId");
        }
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaUpdate criteriaUpdate, EntityMap entityMap, BusOrderAnswer orderAnswer) {
        //是否是预览答案
        Integer preview = entityMap.getInt("preview", 0);
        if (preview.equals(1)) {
            //已作答待审核
            orderAnswer.setAnswerState(1);
        }
    }


    @Override
    public void complete(CriteriaUpdate cu, EntityMap params, BusOrderAnswer orderAnswer) {
        Long answerId = orderAnswer.getAnswerId();
        Integer preview = params.getInt("preview", 0);
        if (preview.equals(1)) {
            BusOrderAnswer busOrderAnswer = busOrderAnswerService.getById(answerId);
            Long orderId = busOrderAnswer.getOrderId();
            BusOrder busOrder = orderService.getById(orderId);
            Integer orderType = busOrder.getOrderType();
            //2.修改订单状态为解答中
            if (OrderTypeEnum.TakePhotosAskQuestions.getValue().equals(orderType)) {
                orderService.updateOrderState(orderId, OrderStatePEnum.OrderState3.getValue());
            }
            if (OrderTypeEnum.BookingTutoring.getValue().equals(orderType)) {
                orderService.updateOrderState(orderId, OrderStateYEnum.OrderState3.getValue());
            }
            if (OrderTypeEnum.HomeWork.getValue().equals(orderType)) {
                orderService.updateOrderState(orderId, OrderStateZEnum.OrderState3.getValue());
            }
        }

    }
}
